package edu.wtbu.cs.androiddemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Map;

import edu.wtbu.cs.androiddemo.R;
import edu.wtbu.cs.androiddemo.activity.store.SPSaveInfo;
import edu.wtbu.cs.androiddemo.activity.store.SaveInfo;
import edu.wtbu.cs.androiddemo.activity.store.FileSaveInfo;

/**
 *
 * Android Studio 4.1之后的版本进行开发时，创建的项目默认的主题所有Button都是Material类型的Button，
 * button通过backgroud修改背景颜色失效
 * 默认使用主题色，所以想要修改颜色，就要把默认主题给关了或替代了。
 */
public class LoginActivity extends AppCompatActivity {

    private static String logTag = LoginActivity.class.getName();

    private TextView tvRegister;
    private TextView tvFindPwd;
    private EditText editName;
    private EditText editPwd;
    private Button btnLogin;
    private String userName;
    private String psw;

    SaveInfo saveInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //todo 切换不同的存储方式
        saveInfo = new SPSaveInfo();
     //   saveInfo = new FileSaveInfo();

        initView();
        initListener();
        Map<String, String> uerInfo = saveInfo.getInfoFromFile(this);
        if (uerInfo != null) {
            editName.setText(uerInfo.get("userName"));
            editPwd.setText(uerInfo.get("userPwd"));
        }
    }

    private void initView() {
        tvRegister = (TextView) findViewById(R.id.tv_register);
        editName = (EditText) findViewById(R.id.et_user_name);
        editPwd = (EditText) findViewById(R.id.et_psw);
        tvRegister = (TextView) findViewById(R.id.tv_register);
        tvFindPwd = (TextView) findViewById(R.id.tv_find_psw);
        btnLogin = (Button) findViewById(R.id.btn_login);
    }

    private void initListener() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userName = editName.getText().toString().trim();
                psw = editPwd.getText().toString();
                if (userName.isEmpty() || psw.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    boolean isSave = saveInfo.saveInfoToFile(LoginActivity.this, userName, psw);
                    if (isSave) {
                        Toast.makeText(LoginActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    //跳转到注册
    public void clickJumpToRegisterButton(View view){
        Log.i(logTag,"clickJumpToRegisterButton");
        Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);
    }
}